<!--
 * @Author: wangjh wangjiahui0710@163.com
 * @Date: 2024-06-18 10:06:27
 * @LastEditors: wangjh wangjiahui0710@163.com
 * @LastEditTime: 2024-07-22 19:09:14
 * @FilePath: \cip-admin\src\pages\park\dual-prevention\risk-management-and-control-list\components\detail-dialog.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<!-- 弹窗 详情 -->
<script lang="tsx" setup>
import FileUpload from "@/components/file-system/file-upload.vue";
import type { FormDescChild } from "@/components/form-desc/form-desc.vue";
import { whManagerBsTbRiskHazardsControlWaringRemindUsingPost } from "@/genapi/wh";
import { useAsync } from "@/hooks/use-async";
import {
	whManagerBsTbRiskHazardsControlGetByIdIdUsingPost,
	whManagerBsTbEnterpriseGetListUsingPost,
} from "@/genapi/wh";
import { computedLoading } from "@/hooks/computed-loading";

import { injectState } from "../state";

defineOptions({ name: "DetailDialog" });

const { page } = injectState()!;

const dialogRef = shallowRef<InstanceType<GlobalComponents["PromiseDialog"]>>();
const formDescRef = shallowRef<InstanceType<GlobalComponents["FormDesc"]>>();

const type = ref<"write" | "read">();

const beforeClose = async () => {
	if (type.value === "write") {
		await ElMessageBox.confirm("确认关闭？");
	}
};

const trigger = async (_type: "write" | "read" = "read", value = {} as any) => {
	type.value = _type;
	model.value = JSON.parse(JSON.stringify(value)) as any;
	await dialogRef.value?.open();
};

defineExpose({
	trigger,
});

const model = ref<(typeof page.records.value)[0]>({});

const file = computed({
	get() {
		if (model.value.fileId) {
			return [
				{
					id: model.value.fileId,
					name: model.value.fileName,
				},
			];
		}
	},
	set(value: any) {
		model.value.fileId = value?.[0]?.id;
	},
});

const title = computed(() => {
	if (type.value === "write") {
		return "预警提示";
	} else {
		return `详情`;
	}
});

const { execute, isLoading } = useAsync(async () => {
	await formDescRef.value?.formRef?.validate();
	await ElMessageBox.confirm("确认提交？", "提示", {
		type: "warning",
	});

	await whManagerBsTbRiskHazardsControlWaringRemindUsingPost({
		data: {
			...model.value,
		},
	});
	ElMessage.success("提交成功");
	page.execute();
	dialogRef.value?.complete();
});

const [state] = computedLoading(async () => {
	const { data } = await whManagerBsTbRiskHazardsControlGetByIdIdUsingPost({
		path: { id: model.value.id },
	});
	return data;
});

const [companyList] = computedLoading(async () => {
	const { data } = await whManagerBsTbEnterpriseGetListUsingPost({ data: {} });
	return data?.map((item) => ({ label: item.name, id: item.id })) ?? [];
});

const getCompanyName = (enterpriseId) => {
	const res = companyList.value.filter((item) => {
		return item.id === enterpriseId;
	});
	return res[0] ? res[0].label : "--";
};

const children: FormDescChild[] = [
	{
		label: "预警内容",
		prop: "waringContent",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 2,
		formItem: () => (
			<el-input
				type="textarea"
				rows={15}
				v-model={model.value.waringContent}
				placeholder="请输入"
			/>
		),
		descItem: () => `${model.value.waringContent ?? "-"}`,
	},
];
</script>

<template>
	<PromiseDialog
		ref="dialogRef"
		:title="title"
		:before-close="beforeClose"
		w="707px!"
	>
		<el-scrollbar h="408px!">
			<FormDesc
				ref="formDescRef"
				m="t-24px x-30px"
				v-if="type === 'write'"
				:type="type === 'read' ? 'desc' : 'form'"
				form-class="gap-x-20px"
				:model="model"
				:children="children"
				:column="2"
			/>
			<div class="detailsDescriptions" v-else>
				<el-descriptions title="风险情况">
					<el-descriptions-item :span="4" label="企业名称">{{
						getCompanyName(state.enterpriseId)
					}}</el-descriptions-item>
					<el-descriptions-item :span="4" label="责任人员">{{
						state.masterName
					}}</el-descriptions-item>
					<el-descriptions-item :span="4" label="风险描述">{{
						state.dangerDesc
					}}</el-descriptions-item>
					<el-descriptions-item :span="4" label="管控措施">{{
						state.handleDesc
					}}</el-descriptions-item>
				</el-descriptions>
				<el-table :data="[state]" style="width: 100%">
					<el-table-column type="index" width="50" />
					<el-table-column label="文字描述" prop="dangerDesc" width="470">
					</el-table-column>
					<el-table-column label="媒体文件" width="120">
						<template #default="scope">
							<img
								src="https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg"
							/>
						</template>
					</el-table-column>
				</el-table>
			</div>
		</el-scrollbar>
		<template v-if="type === 'write'" #footer>
			<el-button @click="dialogRef?.close()">取消</el-button>
			<el-button type="primary" :is-loading="isLoading" @click="execute()"
				>确定</el-button
			>
		</template>
	</PromiseDialog>
</template>
